﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
   public class AddNoteService
    {
       DBHelper db = new DBHelper();

       /// <summary>
       /// 根据便签ID找相对应的便签
       /// </summary>
       /// <param name="noteId">便签ID</param>
       /// <returns>返回一个便签ID</returns>
       public MyNote getMyNoteByNoteId(int noteId)
       {
           MyNote note = new MyNote();
           using (SqlDataReader reader = db.getDataReader("sp_getMyNoteByNoteId", new SqlParameter("@noteId", noteId)))
           {
               if (reader.Read())
               {

                   note.NoteId = noteId;
                   note.NoteTitle = (string)reader[1];
                   note.NoteContent = (string)reader[2];
                   note.CreateTime = (DateTime)reader[3];
                   note.CreateUser.UserId = (int)reader[4];

                   
               }
           }
           db.CloseConnection();
           return note;
       }

       /// <summary>
       /// 根据登陆号来查找登陆用户ID
       /// </summary>
       /// <param name="loginId">登陆号</param>
       /// <returns>登陆用户ID</returns>
       public int getUserIdByLoginId(string loginId)
       {
           return (int)db.getScalar("sp_getUserIdByLoginId",new SqlParameter("@loginId",loginId));
       }

       /// <summary>
       /// 增加便签
       /// </summary>
       /// <param name="note">要增加的便签信息</param>
       /// <returns>返回是否增加成功</returns>
       public int insertMyNote(MyNote note)
       {
           SqlParameter[] values = new SqlParameter[]
           {
               new SqlParameter("@noteTitle",note.NoteTitle),
               new SqlParameter("@noteContent",note.NoteContent),
               new SqlParameter("@createTime",note.CreateTime),
               new SqlParameter("@createUser",note.CreateUser.UserId)
           };
           return db.getCount("sp_insertMyNote",values);
       }

       /// <summary>
       /// 根据便签ID删除便签信息
       /// </summary>
       /// <param name="noteId">便签ID</param>
       /// <returns>返回是否删除成功</returns>
       public int deleteMyNoteByNoteId(int noteId)
       {
           return db.getCount("sp_deleteMyNoteByNoteId", new SqlParameter("@noteId", noteId));
       }

       /// <summary>
       /// 根据便签ID来修改便签信息
       /// </summary>
       /// <param name="note">便签信息</param>
       /// <returns>是否修改成功</returns>
       public int UpdateMyNoteByNoteId(MyNote note)
       {
           SqlParameter[] values = new SqlParameter[]
           {
               new SqlParameter("@noteTitle",note.NoteTitle),
               new SqlParameter("@noteContent",note.NoteContent),
               new SqlParameter("@createTime",note.CreateTime),
               new SqlParameter("@createUser",note.CreateUser.UserId),
               new SqlParameter("@noteId",note.NoteId)
           };
           return db.getCount("sp_UpdateMyNoteByNoteId",values);
       }
    }
}
